package com.ruoyi.petSys.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.petSys.entity.dto.BrowseHistoryDto;
import com.ruoyi.petSys.entity.vo.BrowseHistoryVo;
import com.ruoyi.petSys.mapper.InvitationMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.petSys.mapper.BrowseHistoryMapper;
import com.ruoyi.petSys.domain.BrowseHistory;
import com.ruoyi.petSys.service.IBrowseHistoryService;

/**
 * 浏览记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-17
 */
@Service
public class BrowseHistoryServiceImpl implements IBrowseHistoryService 
{
    @Autowired
    private BrowseHistoryMapper browseHistoryMapper;

    /**
     * 查询浏览记录
     * 
     * @param id 浏览记录主键
     * @return 浏览记录
     */
    @Override
    public BrowseHistoryVo selectBrowseHistoryById(Long id)
    {
        return browseHistoryMapper.selectBrowseHistoryById(id);
    }

    /**
     * 查询浏览记录列表
     * 
     * @param browseHistory 浏览记录
     * @return 浏览记录
     */
    @Override
    public List<BrowseHistoryVo> selectBrowseHistoryList(BrowseHistoryDto browseHistory)
    {

        List<BrowseHistoryVo> browseHistoryVos = browseHistoryMapper.selectBrowseHistoryList(browseHistory);
        browseHistoryVos = browseHistoryVos.stream().peek(browseHistoryVo -> {
            if(StringUtils.isNotBlank(browseHistoryVo.getImgUrl().trim())){
                List<String> imgList = Arrays.stream(browseHistoryVo.getImgUrl().split(";")).collect(Collectors.toList());

                browseHistoryVo.setImgUrlList(imgList);
            }else{
                browseHistoryVo.setImgUrlList(new ArrayList<>());
            }
            if("/dev-api".equals(browseHistoryVo.getAvatar())){
                browseHistoryVo.setAvatar("/icon/profile.jpg");
            }


        }).collect(Collectors.toList());
        return browseHistoryVos;
    }

    /**
     * 新增浏览记录
     * 
     * @param browseHistory 浏览记录
     * @return 结果
     */
    @Override
    public int insertBrowseHistory(BrowseHistory browseHistory)
    {
        BrowseHistoryDto browseHistoryDto = new BrowseHistoryDto();
        browseHistoryDto.setId(browseHistory.getId());
        browseHistoryDto.setUserId(browseHistory.getUserId());
        browseHistoryDto.setInviId(browseHistory.getInviId());
        List<BrowseHistoryVo> browseHistoryVos = selectBrowseHistoryList(browseHistoryDto);
        browseHistory.setBrowseTime(DateUtils.getNowDate());

        if(browseHistoryVos.size() == 0){

        return browseHistoryMapper.insertBrowseHistory(browseHistory);
        }else{
            return browseHistoryMapper.updateBrowseTime(browseHistory);
        }

    }

    /**
     * 修改浏览记录
     * 
     * @param browseHistory 浏览记录
     * @return 结果
     */
    @Override
    public int updateBrowseHistory(BrowseHistory browseHistory)
    {
        browseHistory.setUpdateTime(DateUtils.getNowDate());
        return browseHistoryMapper.updateBrowseHistory(browseHistory);
    }

    /**
     * 批量删除浏览记录
     * 
     * @param ids 需要删除的浏览记录主键
     * @return 结果
     */
    @Override
    public int deleteBrowseHistoryByIds(Long[] ids)
    {
        return browseHistoryMapper.deleteBrowseHistoryByIds(ids);
    }

    /**
     * 删除浏览记录信息
     * 
     * @param id 浏览记录主键
     * @return 结果
     */
    @Override
    public int deleteBrowseHistoryById(Long id)
    {
        return browseHistoryMapper.deleteBrowseHistoryById(id);
    }
}
